package com.doit.day06

import scala.util.Random

object _06_模式匹配 {
  def main(args: Array[String]): Unit = {
    //值的匹配
    /*val name:String = "思怡"
    name match {
      case "婷婷" => println("我不是思怡，我是婷婷，比她漂亮多了")
      case "涵涵" => println("我不是思怡，我是涵涵，我比他有钱多了")
      case "zss" => println("老子张三，人送外号，法外狂徒")
      case "思怡" => println("我要你的，虽然长得丑")//我要你的，虽然长得丑
      case _ => println("看来思怡没人要啊！！！！")//看来思怡没人要啊！！！！
    }
    val score = 59
    val res: String = score match {
      case 100 => {
        //去数据库里面取数据
        //然后对他进行处理
        //处理完了又写回去了
        "满分"}
      case 80 =>  "优秀"
      case 60 =>  "及格"
      case _ =>   "不及格"
    }
    println(res)*/


    //模式守卫
   /* val num: Int = new Random().nextInt(100)
    val str: String = num match {
      case num if num >= 90 => "有请浩南唱首歌" + (num * 100)
      case num if num >= 80 => "有请婷婷唱首歌" + (num * 100)
      case num if num >= 70 => "有请涵涵唱首歌" + (num * 100)
      case num if num >= 60 => "有请凯哥唱首歌" + (num * 100)
      case num if num >= 50 => "有请浩哥唱首歌" + (num * 100)
      case num if num >= 40 => "有请舒哥唱首歌" + (num * 100)
      case num if num >= 30 => "有请思怡唱首歌" + (num * 100)
      case _ => "有请下一位选手，杰哥选人"
    }

    println(str)


    //偏函数
    val pf: PartialFunction[Any, Int] = new PartialFunction[Any, Int] {
      //这个方法就类似于filter
      override def isDefinedAt(x: Any) = x.isInstanceOf[Int]

      //这个方法就类似于map
      override def apply(v1: Any) = v1.asInstanceOf[Int] * 10
    }

    val list: List[Any] = List(1, "zss", 2, 'c', true)

    val ints: List[Int] = list.collect(pf)

    val list1: List[Any] = list.collect({
      //匹配类型
      case x: Int => x * x
      case x: String => x.toUpperCase()
      case _ =>
    })

    list1.foreach(println)*/

    //匹配类型
/*    val list: List[Any] = List(1, "zss", 1.9, true, 'c')
    for (elem <- list) {
      elem match {
        case a:Int => println("这是一个Int类型就会到这个分支里面来"+ (a*10))
        case a:Double => println("这是一个Double类型就会到这个分支里面来"+ (a*10))
        case a:String => println("这是一个String类型就会到这个分支里面来"+ a.toUpperCase())
        case a:Boolean => println("这是一个bool类型就会到这个分支里面来"+ !a)
        case _ => println("你这个类型，我不要")
      }*/

    /**
     * list array set 这些集合的类型能不能匹配==》 能匹配
     * 但是除了array类型的泛型他能识别出来
     * List  set 泛型能被识别吗？ ==》 泛型的擦除
     */

    //匹配集合类型  注意泛型会被擦除
/*      val list1: List[Any] = List(Array(1, 2, 3), List(1, 2, 3), Array("aa", "bb"), List("aa", "bb"), Set(1, 2, 3), Set("aa", "bb"))
      for (elem <- list1) {
        elem match {
          case a:Array[Int] => println("int类型的数组" + a.mkString(","))
          case a:Array[String] => println("String类型的数组" + a.mkString(","))
          case a:List[Int] => println("int类型的List" + a.mkString(","))
          case a:List[String] => println("String类型的List" + a.mkString(","))
          case a:Set[Int] => println("int类型的set" + a.mkString(","))
          case a:Set[String] => println("String类型的Set" + a.mkString(","))
          case _ => println("啥也不是")
        }
      }*/


    //匹配数组
    val list: List[Array[Int]] = List(Array(1, 2, 3), Array(1,2,4),Array(1, 2, 3, 4), Array(2, 3), Array(2, 3, 4))
    for (elem <- list) {
      elem match {
        case Array(1,2,3) => println("匹配的数组必须得是三个元素，并且三个元素必须得是1，2 3")
        case Array(_,2,_) => println("匹配的数组必须得是三个元素，并且第二个元素必须是2")
        case Array(2,_*) => println("匹配的数组至少有一个元素，并且第一个必须是2")
        case Array(_,_,_,_) => println("匹配的是四个元素的数组，具体元素是多少无所谓")
        case _ => println("呵呵")
      }
    }


    //匹配元祖
    for (tuple <- Array((0, 1), (1, 0), (1, 1), (1, 0, 2))) {
      val result = tuple match {
        case (0, _) => "0 ..." //是第一个元素是 0 的元组
        case (y, 0) => "" + y + "0" // 匹配后一个元素是 0 的对偶元组
        case (a, b) => "" + a + " " + b
        case (_,_,_) => "三元元祖"
        case _ => "something else" //默认
      }
      println(result)
    }


    //匹配对象  解构匹配  必须重写unapply方法 只有两个地方有   样例类   伴生类伴生对象
    val zss: Car1 = new Car1(1, "zss", 18, "male")
    val str: String = zss match {
      case Car1(a, b, 28, "male") => "a"
      case Car1(2, "lss", 28, "male") => "b"
      case Car1(3, "lss", 28, "male") => "c"
      case Car1(1, "zss", 18, "male") => "d"
      case _ => "e"
    }

    println(str)


    //解构匹配他还有特殊的妙用  ==》 不过一般没人用

    val (id,name) = (1,"zss")
    val id1 = 1
    val name1 = "zss"

    println(id)
    println(name)


    val map: Map[Int, String] = Map((1, "zss"), (2, "lss"), (3, "ww"))

    for ((_,name2) <- map) {
/*      val id: Int = elem._1
      val name: String = elem._2*/
      println(name2.toUpperCase())
    }






  }

}

case class Car(id:Int,name:String,age:Int,gender:String)


class Car1(val id:Int,val name:String,val age:Int,val gender:String)

object Car1{

//  def apply(id: Int, name: String, age: Int, gender: String): Car1 = new Car1(id, name, age, gender)

  def unapply(arg: Car1): Option[(Int, String, Int, String)] = Some(arg.id,arg.name,arg.age,arg.gender)

}